<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 编程第一步</h1>
        <p>在前面的教程中我们已经学习了一些 Python3 的基本语法知识，下面我们尝试来写一个斐波纳契数列。</p>
        <p>实例如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># Fibonacci series: 斐波纳契数列</span><span class="pln">
</span><span class="com"># 两个元素的总和确定了下一个数</span><span class="pln">
a</span><span class="pun">,</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span
                class="pln">
</span><span class="kwd">while</span><span class="pln"> b </span><span class="pun">&lt;</span><span class="pln"> </span><span
                class="lit">10</span><span class="pun">:</span><span class="pln">
    </span><span class="kwd">print</span><span class="pun">(</span><span class="pln">b</span><span
                class="pun">)</span><span class="pln">
    a</span><span class="pun">,</span><span class="pln"> b </span><span class="pun">=</span><span
                class="pln"> b</span><span class="pun">,</span><span class="pln"> a</span><span
                class="pun">+</span><span class="pln">b</span></pre>
        <p>执行以上程序，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pln">
</span><span class="lit">1</span><span class="pln">
</span><span class="lit">2</span><span class="pln">
</span><span class="lit">3</span><span class="pln">
</span><span class="lit">5</span><span class="pln">
</span><span class="lit">8</span></pre>
        <p>
            这个例子介绍了几个新特征。
        </p>
        <p>第一行包含了一个复合赋值：变量 a 和 b 同时得到新值 0 和 1。最后一行再次使用了同样的方法，可以看到，右边的表达式会在赋值变动之前执行。右边表达式的执行顺序是从左往右的。</p>
        <p><strong>输出变量值:</strong></p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> i </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">256</span><span class="pun">*</span><span class="lit">256</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">'i 的值为：'</span><span class="pun">,</span><span
                class="pln"> i</span><span class="pun">)</span><span class="pln">
i </span><span class="pun">的值为：</span><span class="pln"> </span><span class="lit">65536</span></pre>
        <h3>end 关键字</h3>
        <p>关键字end可以用于将结果输出到同一行，或者在输出的末尾添加不同的字符，实例如下：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># Fibonacci series: 斐波纳契数列</span><span class="pln">
</span><span class="com"># 两个元素的总和确定了下一个数</span><span class="pln">
a</span><span class="pun">,</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span
                class="pln">
</span><span class="kwd">while</span><span class="pln"> b </span><span class="pun">&lt;</span><span class="pln"> </span><span
                class="lit">1000</span><span class="pun">:</span><span class="pln">
    </span><span class="kwd">print</span><span class="pun">(</span><span class="pln">b</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">end</span><span
                class="pun">=</span><span class="str">','</span><span class="pun">)</span><span class="pln">
    a</span><span class="pun">,</span><span class="pln"> b </span><span class="pun">=</span><span
                class="pln"> b</span><span class="pun">,</span><span class="pln"> a</span><span
                class="pun">+</span><span class="pln">b</span></pre>
        <p>执行以上程序，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pun">,</span><span
                class="lit">1</span><span class="pun">,</span><span class="lit">2</span><span class="pun">,</span><span
                class="lit">3</span><span class="pun">,</span><span class="lit">5</span><span class="pun">,</span><span
                class="lit">8</span><span class="pun">,</span><span class="lit">13</span><span class="pun">,</span><span
                class="lit">21</span><span class="pun">,</span><span class="lit">34</span><span
                class="pun">,</span><span class="lit">55</span><span class="pun">,</span><span
                class="lit">89</span><span class="pun">,</span><span class="lit">144</span><span
                class="pun">,</span><span class="lit">233</span><span class="pun">,</span><span
                class="lit">377</span><span class="pun">,</span><span class="lit">610</span><span
                class="pun">,</span><span class="lit">987</span><span class="pun">,</span></pre>

    </div>
</div>
</body>
</html>
